package com.example.mask.maskmobilesshop.controller;

import com.example.mask.maskmobilesshop.basic.BasicController;
import com.example.mask.maskmobilesshop.entity.Member;
import com.example.mask.maskmobilesshop.enums.HttpStatus;
import com.example.mask.maskmobilesshop.service.IMemberService;
import com.example.mask.maskmobilesshop.service.impl.MemberServiceImpl;
import com.example.mask.maskmobilesshop.utils.StringUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录的Controller
 *
 * @author anhuifix
 * @email anhuifix@163.com
 */
@RestController
@RequestMapping(value = "/member")
public class LoginController extends BasicController {


    private Logger logger = LoggerFactory.getLogger(LoginController.class.getName());
    @Autowired
    private IMemberService iMemberService;





    @ApiOperation(value = "会员登录接口", notes = "会员根据手机号码及手机验证码登录APP,返回:1002-验证码不正确 2000-成功 2001-失败 1001-验证码已过期 1000-会员不存在 3000-传入参数不正确")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "用户手机号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "passwordAge", value = "再次输入密码", required = true, dataType = "String", paramType = "query")})
    @RequestMapping(value = "/registerMember", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String, Object> registerMember(String mobile, String password, String passwordAge) {
        Map<String, Object> result = new HashMap<>();
        try {
            if(!StringUtils.isEmpty(mobile) && !StringUtils.isEmpty(password) && !StringUtils.isEmpty(passwordAge)){
                //判断是否存在会员了
                Boolean isSame =  iMemberService.findBySame(mobile);
                if(isSame){
                    result.put("message","已经存在账号了！");
                    result.put("status", HttpStatus.SUCCESS.getValue());
                    return result;
                }else{
                    if(password.equals(passwordAge)){
                        Member member  =new Member();
                        member.setCreateTime(new Date());
                        member.setCreateUser("admin");
                        member.setUpdateTime(new Date());
                        member.setUpdateUser("admin");
                        member.setMobile(mobile);
                        member.setNickName("c"+mobile);
                        member.setBirthday(new Date());
                        member.setPassword(password);
                        member.setBackUrl(null);
                        member.setImageUrl(null);
                        member.setLastLoginTime(new Date());
                        member.setSex(1);
                        member.setState(1);
                        member.setPointId(Long.parseLong("1"));
                        member.setRealName("c"+mobile);
                        //进行新增会员
                        boolean insert = iMemberService.insert(member);
                        if(insert){
                            result.put("message","注册成功！");
                            result.put("status", HttpStatus.SUCCESS.getValue());
                        }else{
                            result.put("message","注册失败！");
                            result.put("status", HttpStatus.SUCCESS.getValue());
                        }

                    }else{
                        result.put("message","输入的密码两次不相同！");
                        result.put("status", HttpStatus.SUCCESS.getValue());
                    }
                }
            }

        }catch (Exception e){
            result.put("status", HttpStatus.INTERNAL_SERVER_ERROR.getValue());
            logger.error("注册用户失败,错误堆栈轨迹如下:");
            e.printStackTrace();
        }
        return result;
    }



    @ApiOperation(value = "会员登录接口", notes = "会员根据手机号码及手机验证码登录APP,返回:1002-验证码不正确 2000-成功 2001-失败 1001-验证码已过期 1000-会员不存在 3000-传入参数不正确")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "用户手机号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query") })
    @RequestMapping(value = "/loginMember", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String, Object> loginMember(String mobile, String password) {


        return  null;
    }
}
